//
//  RHBleScanCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/25.
//

#import "RHBleScanCell.h"
#import "CBLEDataMnger.h"

@interface RHBleScanCell()
@property (nonatomic ,strong) QMUILabel *devNameLab;

@property (nonatomic ,strong) UIImageView *rssiImg;

@property (nonatomic ,strong) QMUILabel *typeContentLab;

@property (nonatomic ,strong) QMUILabel *macContentLab;
@end

@implementation RHBleScanCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    
    return self;
}

- (void)setDataInfo:(CDataPheralInfo *)dataInfo{
    
    _dataInfo = dataInfo;
    
    NSNumber *RSSI = _dataInfo.RSSI;
    BOOL isName = ([dataInfo.devName isEqualToString:@""] || dataInfo.devName == nil);
    self.macContentLab.text = dataInfo.adverMacAddr;
    self.typeContentLab.text = ValidNullDataToString(dataInfo.deviceType, @"--");
    
    /*计算蓝牙距离*/
    int iRssi = abs([RSSI intValue]);
    self.devNameLab.text = ValidNullDataToString(dataInfo.devName, @"unknow");
    
    if (iRssi < 55) {
        [self.rssiImg setImage:[UIImage imageNamed:@"device_rssi4"]];
    }
    else if(iRssi < 70){
        [self.rssiImg setImage:[UIImage imageNamed:@"device_rssi3"]];
    }
    else if(iRssi < 80 ){
        [self.rssiImg setImage:[UIImage imageNamed:@"device_rssi2"]];
    }else{
        [self.rssiImg setImage:[UIImage imageNamed:@"device_rssi1"]];
    }
    
   
    
    
    
    self.macContentLab.text = ValidNullDataToString(dataInfo.adverMacAddr, @"--");
    
}

- (void)setupUI{
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:kRGB(246, 248, 250)];
    bgView.layer.cornerRadius = 8;
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    kWeakSelf;
    _devNameLab = [[QMUILabel alloc] init];
    [_devNameLab setText:@"--"];
    [_devNameLab setTextAlignment:NSTextAlignmentCenter];
    [_devNameLab setFont:[UIFont systemFontOfSize:15]];
    [_devNameLab setTextColor:FontColor(@"#999999")];
    [bgView addSubview:_devNameLab];
    
    [_devNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
    }];
    
    _rssiImg = [[UIImageView alloc] init];
    [_rssiImg setImage:[UIImage imageNamed:@"device_rssi4"]];
    [bgView addSubview:_rssiImg];
    [_rssiImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(weakSelf.devNameLab);
        make.right.mas_offset(-15);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kRGB(235, 237, 240);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.top.equalTo(weakSelf.devNameLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    
    QMUILabel *typeTitleLab = [[QMUILabel alloc] init];
    [typeTitleLab setText:[NSString stringWithFormat:@"%@:",@"类型".rh_localized]];
    [typeTitleLab setTextAlignment:NSTextAlignmentCenter];
    [typeTitleLab setFont:kSYSTEMFONT(13)];
    [typeTitleLab setTextColor:kColorRGBA(63, 68, 67, 0.8)];
    [bgView addSubview:typeTitleLab];
    
    [typeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(line.mas_bottom).offset(18);
    }];
    
    _typeContentLab = [[QMUILabel alloc] init];
    [_typeContentLab setText:@"--"];
    [_typeContentLab setTextAlignment:NSTextAlignmentCenter];
    [_typeContentLab setFont:kSYSTEMFONT(12)];
    [_typeContentLab setTextColor:kColorRGBA(63, 68, 67, 1)];
    [bgView addSubview:_typeContentLab];
    
    [_typeContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(typeTitleLab);
    }];
    
    QMUILabel *macTitleLab = [[QMUILabel alloc] init];
    [macTitleLab setText:[NSString stringWithFormat:@"%@:",@"MAC".rh_localized]];
    [macTitleLab setTextAlignment:NSTextAlignmentCenter];
    [macTitleLab setFont:kSYSTEMFONT(13)];
    [macTitleLab setTextColor:kColorRGBA(63, 68, 67, 0.8)];
    [bgView addSubview:macTitleLab];
    
    [macTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(typeTitleLab);
        make.top.equalTo(typeTitleLab.mas_bottom).offset(18);
        make.bottom.mas_offset(-18);
    }];
    
    _macContentLab = [[QMUILabel alloc] init];
    [_macContentLab setText:@"--"];
    [_macContentLab setTextAlignment:NSTextAlignmentCenter];
    [_macContentLab setFont:kSYSTEMFONT(12)];
    [_macContentLab setTextColor:kColorRGBA(63, 68, 67, 1)];
    [bgView addSubview:_macContentLab];
    
    [_macContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(macTitleLab);
    }];
}

@end
